package com.shrfid.rfidsdk.qr;


import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;

import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialProber;
import com.hoho.android.usbserial.util.SerialInputOutputManager;
import com.rfid.common.log.LogUtils;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public enum QRCodeManager {
    INSTANCE;

    private static final String TAG = "QRCodeManager";

    private IOnQrListener onQrListener;

    private UsbSerialDriver sDriver;

    private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();

    private SerialInputOutputManager mSerialIoManager;

    private final SerialInputOutputManager.Listener mListener = new SerialInputOutputManager.Listener() {

        @Override
        public void onRunError(Exception e) {
            Log.d(TAG, "Runner stopped.");
        }

        @Override
        public void onNewData(final byte[] data) {
            String qr = new String(data);
            LogUtils.v("二维码：" + qr);
            if (onQrListener != null) {
                onQrListener.onQr(qr);
            }
        }
    };

    public void init(Context context) {
        UsbManager mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        for (final UsbDevice device : mUsbManager.getDeviceList().values()) {
            if (device.getVendorId() == 0x2dd6 && device.getProductId() == 0x26ca) {
                final List<UsbSerialDriver> drivers = UsbSerialProber.probeSingleDevice(mUsbManager, device);
                if (!drivers.isEmpty()) {
                    UsbSerialDriver driver = drivers.get(0);
                    open(driver);
                    break;
                }
            }
        }
    }

    private void open(UsbSerialDriver driver) {
        sDriver = driver;
        try {
            sDriver.open();
            sDriver.setParameters(115200, 8, UsbSerialDriver.STOPBITS_1, UsbSerialDriver.PARITY_ODD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        stopIoManager();
        startIoManager();
    }

    private void stopIoManager() {
        if (mSerialIoManager != null) {
            Log.i(TAG, "Stopping io manager ..");
            mSerialIoManager.stop();
            mSerialIoManager = null;
        }
    }

    private void startIoManager() {
        if (sDriver != null) {
            Log.i(TAG, "Starting io manager ..");
            mSerialIoManager = new SerialInputOutputManager(sDriver, mListener);
            mExecutor.submit(mSerialIoManager);
        }
    }


    public void close() {
        try {
            if (sDriver != null) {
//                sDriver.close();
                sDriver = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setOnQrListener(IOnQrListener onQrListener) {
        this.onQrListener = onQrListener;
    }


    public interface IOnQrListener {
        void onQr(String qr);
    }
}
